package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.bean.IndexModel;
import com.apps.ftapp.custom.NoScrollGridView;
import com.apps.ftapp.ui.ExamActivity;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lyli on 2017/11/2.
 */

public class ZhenTiNewAdapter extends BaseAdapter {


    private Context context;
    private List<IndexModel.IndexBean> dataList;
    private LayoutInflater layoutInflater;

    public ZhenTiNewAdapter(Context context, List<IndexModel.IndexBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);

    }


    public void addRes(List<IndexModel.IndexBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
//        LianXiModel.ChildQuestionCategoryBean bean = dataList.get(i);


        final IndexModel.IndexBean bean = dataList.get(i);

        switch (getItemViewType(i)) {


            //TAB选项
            case 0:

                L.i("main", "==========");
                ViewHolder viewHolder;
                if (view == null) {
                    view = this.layoutInflater.inflate(R.layout.group_list_item_tag, null);
                    viewHolder = new ViewHolder(view);
                    view.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) view.getTag();
                }

                viewHolder.group_list_item_text.setText(bean.getTitle());

                break;


            case 1:

                ViewHolder2 viewHolder1;
                if (view == null) {
                    view = this.layoutInflater.inflate(R.layout.group_list_item, null);
                    viewHolder1 = new ViewHolder2(view);
                    view.setTag(viewHolder1);
                } else {
                    viewHolder1 = (ViewHolder2) view.getTag();
                }


                viewHolder1.nianfen.setText(bean.getTitle());
                final ZhenTiGridAdapter mainGrdiAdapter1 = new ZhenTiGridAdapter(context, bean.getDataList());
                viewHolder1.gridView.setAdapter(mainGrdiAdapter1);

                viewHolder1.nianfen.setText(bean.getTitle());

                viewHolder1.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Map d = bean.getDataList().get(position);
                        HashMap map = new HashMap();
                        map.put("id", Tools.isNull(d.get("id")));
                        ActivitySkipUtil.skipAnotherForActivity(context, ExamActivity.class, map);
//
                    }
                });

//
//                View view1 = LayoutInflater.from(context).inflate(R.layout.zhenti_dange, null);
//                L.i("main","bean.getDataList():::"+bean.getDataList().toString());
//                for(final Map d:bean.getDataList()){
//
//                    L.i("main","水王田口。。。。    11。");
//                    TextView biaoti = (TextView)view1.findViewById(R.id.biaoti);
//                    biaoti.setText(Tools.isNull(d.get("title")));
//                    viewHolder1.linearLayout.addView(view1);
//
//                    view1.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            HashMap map = new HashMap();
//                            map.put("id",Tools.isNull(d.get("id")));
//                            ActivitySkipUtil.skipAnotherForActivity(context, ExamActivity.class,map);
//
//                        }
//                    });
//
//                }


                break;
        }


//        viewHolder.zhentiTitle.setText(beann.getName());
//
//        if("1".equals(Tools.isNull(bean.getAlready()))){
//
//            viewHolder.zhentiBtn.setText("已做过");
//        }else{
//            viewHolder.zhentiBtn.setText("未做过");
//        }

        return view;
    }


    //不同的item布局类型的数量
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    //不同布局的item的类型
    @Override
    public int getItemViewType(int position) {
        L.i("main", "dataList.get(position).getType()" + dataList.get(position).getType());
        return dataList.get(position).getType();
    }


//    class GridBookViewHolder {
//        public GridView gridView;
//
//        public GridBookViewHolder(View view) {
//            gridView = (GridView) view.findViewById(R.id.bookGridView);
//        }
//
//    }


    class ViewHolder {
        TextView group_list_item_text;

        public ViewHolder(View view) {
            group_list_item_text = view.findViewById(R.id.group_list_item_text);
        }

    }


    class ViewHolder2 {
        TextView nianfen;
        public NoScrollGridView gridView;

        public ViewHolder2(View view) {
            nianfen = view.findViewById(R.id.nianfen);
            gridView = view.findViewById(R.id.gridview);
        }
    }


}
